/*
 * Date         Author        Note
 * 2015.05.26   wzwz          程式编辑
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mh/common.h>
#include <mh/sram.h>
#include <mh/mac.h>
#include "knit.h"
#include <mh/mh_file.h>
#include "decript.h"
#include "prefixknit.h"
#include "backpiece.h"


/*
 *
 */
void SafeFreeAuxConList(MH_KNIT *p)
{
    int i = 0;
    p->aux_list.index = -1;
    for(i = 0; i < p->aux_list.num; ++i)
    {
        if(p->aux_list.paux_con[i].pcon)
        {
            free(p->aux_list.paux_con[i].pcon);
            p->aux_list.paux_con[i].pcon = 0;
        }
    }
    p->aux_list.num = 0;
    free(p->aux_list.paux_con);
    p->aux_list.paux_con = 0;

}
/*
 *  brief: 创建副程式链表
 *  param: pcon_buf:副程式所有数据
 * return: 副程式的个数
 *   date: 2015\05\26
 * author: haoyaoyao
 *
 */
int CreateAuxConList(MH_KNIT *p, KNIT_PAGE *pcon_buf, ushort total)
{
    int i = 0;
    int rows = 0;
    int count = 0;
    p->aux_list.index = -1;
    p->aux_list.paux_con = calloc(total, sizeof(AUX_CON));

    for(i = 0; i < total; ++i)
    {
        KNIT_PAGE *buf = pcon_buf + i;
        rows++;
        if((buf->end.end == 1) || (buf->end.end == 3))
        {
            p->aux_list.paux_con[count].rows = rows;
            p->aux_list.paux_con[count].pcon = calloc(1, rows * sizeof(KNIT_PAGE));
            memcpy(p->aux_list.paux_con[count].pcon, pcon_buf + (i + 1 - rows), rows * sizeof(KNIT_PAGE));
            ++count;
            rows = 0;
        }
    }
    p->aux_list.num = count;
    return count;
}
/*
 *
 */
int GetAuxConTotalRows(MH_KNIT *p)
{
    int i = 0;
    int ret = 0;
    for(i = 0; i < p->aux_list.num; ++i)
    {
        ret += p->aux_list.paux_con[i].rows;
    }
    return ret;
}
/*
 *
 */
void GetAuxConData(MH_KNIT *p, KNIT_PAGE *pcon, int total)
{
    int i = 0;
    int count = 0;
    for(i = 0; i < p->aux_list.num; ++i)
    {
        ushort rows = p->aux_list.paux_con[i].rows;
        if(count + rows <= total)
        {
            memcpy(&pcon[count], p->aux_list.paux_con[i].pcon, rows * sizeof(KNIT_PAGE));
        }
        else
        {
            printf("%s: %d fatal error\n", __FILE__, __LINE__);
        }
        count += rows;
    }
}
/*
 * line: start from 0
 */
int GetAuxConIndex(MH_KNIT *p, ushort line)
{
    int ret = -1;
    if(p->aux_list.num)
    {
        uchar index = p->knit_page[line].sub_prg;
        if(index > 0 && index <= p->aux_list.num)
        {
            ret = index - 1;
            printf ("subprg: %d at %d\n", index, line + 1);
        }
    }
    return ret;
}
/*
 *
 */
int IsAuxConKnit(MH_KNIT *p)
{
    int ret = FALSE;
    if(p->aux_list.index > -1 && p->aux_list.index < p->aux_list.num)
    {
        ret = TRUE;
    }
    return ret;
}
/*
 *
 */
ushort GetAuxConRows(MH_KNIT *p)
{
    ushort ret = 0;
    if(p->aux_list.index > -1 && p->aux_list.index < p->aux_list.num)
    {
        ret = p->aux_list.paux_con[p->aux_list.index].rows;
    }
    return ret;
}
/*
 * line: start from 1
 */
void GetAuxConPage(MH_KNIT *p, KNIT_PAGE *pcon, int index, ushort line)
{
    if(index > -1 && index < p->aux_list.num)
    {
        if(line > 0 && line <= p->aux_list.paux_con[index].rows)
        {
            memcpy(pcon, &p->aux_list.paux_con[index].pcon[line - 1], sizeof(KNIT_PAGE));
        }
    }
}
/*
 * ret: start from 0
 */
int GetAuxConCurtIndex(MH_KNIT *p)
{
    return p->aux_list.index;
}
/*
 * index: start from 0
 */
void SetAuxConCurtIndex(MH_KNIT *p, int index)
{
    p->aux_list.index = index;
}
/*
 * ret start from 1
 */
ushort GetAuxConCurtLine(MH_KNIT *p)
{
    return p->aux_list.line + 1;
}
/*
 * line: start from 1
 */
void SetAuxConCurtLine(MH_KNIT *p, ushort line)
{
    p->aux_list.line = line - 1;
}

